﻿namespace Web.Admin.Admins
{
    using System;
    using System.Collections.Specialized;
    using System.Web.Security;
    using System.Web.UI.WebControls;

    public class MembershipFactory
    {

        public static MembershipUserCollection FindUsersByName(string usernameToMatch)
        {
            return FindUsersByName(usernameToMatch, Int32.MaxValue, 0);
        }

        public static MembershipUserCollection FindUsersByName(string usernameToMatch, int maximumRows, int startRowIndex)
        {
            int pageIndex = (startRowIndex / maximumRows);
            int totalRecords = 0;
            if (String.IsNullOrEmpty(usernameToMatch))
                return Membership.GetAllUsers(pageIndex, maximumRows, out totalRecords);
            else
            {
                if (maximumRows != Int32.MaxValue)
                    usernameToMatch = usernameToMatch + "%";
                return Membership.FindUsersByName(usernameToMatch, pageIndex, maximumRows, out totalRecords);
            }
        }

        public static int FindUsersByNameCount(string usernameToMatch)
        {
            int totalRecords = 0;
            if (String.IsNullOrEmpty(usernameToMatch))
                Membership.GetAllUsers(0, 1, out totalRecords);
            else
                Membership.FindUsersByName(usernameToMatch + "%", 0, 1, out totalRecords);
            return totalRecords;
        }
        public static void UpdateUser(IOrderedDictionary parameters, ListItemCollection roles)
        {
            MembershipUser user = Membership.GetUser(((string)(parameters["UserName"])));
            if (user != null)
            {
                user.Comment = ((string)(parameters["Comment"]));
                user.Email = ((string)(parameters["Email"]));
                user.IsApproved = ((bool)(parameters["IsApproved"]));
                Membership.UpdateUser(user);
                UpdateRoles(user.UserName, roles);
            }
        }

        public static void CreateUser(IOrderedDictionary parameters, ListItemCollection roles)
        {
            MembershipCreateStatus status = MembershipCreateStatus.Success;
            MembershipUser user = Membership.CreateUser(((string)(parameters["UserName"])), ((string)(parameters["Password"])), ((string)(parameters["Email"])), ((string)(parameters["PasswordQuestion"])), ((string)(parameters["PasswordAnswer"])), ((bool)(parameters["IsApproved"])), out status);
            if (status == MembershipCreateStatus.DuplicateEmail)
                throw new Exception("Duplicate email address.");
            if (status == MembershipCreateStatus.DuplicateUserName)
                throw new Exception("Duplicate user name.");
            if (status == MembershipCreateStatus.InvalidAnswer)
                throw new Exception("Password answer is not valid");
            if (status == MembershipCreateStatus.InvalidEmail)
                throw new Exception("E-mail is not valid.");
            if (status == MembershipCreateStatus.InvalidPassword)
                throw new Exception("Password is invalid. Password length minimum: 7. Non-alphanumeric characters requ" +
                        "ired: 1.");
            if (status == MembershipCreateStatus.InvalidQuestion)
                throw new Exception("Password question is not valid.");
            if (status == MembershipCreateStatus.InvalidUserName)
                throw new Exception("User name is not valid.");
            if (status == MembershipCreateStatus.UserRejected)
                throw new Exception("User has been rejected.");
            UpdateRoles(user.UserName, roles);
        }

        public static void DeleteUser(string username)
        {
            Membership.DeleteUser(username, true);
        }

        public static void UpdateRoles(string username, ListItemCollection listOfRoles)
        {
            string[] userRoles = Roles.GetRolesForUser(username);
            foreach (ListItem item in listOfRoles)
            {
                if (item.Selected)
                {
                    if (Array.IndexOf(userRoles, item.Value) == -1)
                        Roles.AddUsersToRole(new string[] {
                                    username}, item.Value);
                }
                else
                    if (Array.IndexOf(userRoles, item.Value) != -1)
                        Roles.RemoveUserFromRole(username, item.Value);
            }
        }
    }
}
